/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nhadat.action;

import java.io.File;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import nhadat.action.form.LoginActionForm;
import nhadat.action.form.UpdateActionForm;
import nhadat.xml.DatabaseAndXMLFileMappingSyncrolization;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

/**
 *
 * @author huanht
 */
public class UpdateAction extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        request.setCharacterEncoding("UTF-8");


        UpdateActionForm f = (UpdateActionForm) form;
        LoginActionForm loginForm = (LoginActionForm) request.getSession().getAttribute("LoginActionForm");
        String username = loginForm.getTendangnhap();
        FormFile myFile = f.getTheFile();
        String fileName = myFile.getFileName();
        String filePath = getServlet().getServletContext().getRealPath("/") + "images\\";

        if (!fileName.equals("")) {
            System.out.println("Server path:" + filePath);
            File fileToCreate = new File(filePath, fileName);
            if (!fileToCreate.exists()) {
                FileOutputStream fileOutStream = new FileOutputStream(fileToCreate);
                fileOutStream.write(myFile.getFileData());
                fileOutStream.flush();
                fileOutStream.close();
            }
        }
        if (!fileName.equals("")) {
            f.UpdateToXMLFull("images/" + fileName, username);
        } else {
            f.UpdateToXMLFull(" ", username);
        }
        if (!fileName.equals("")) {
            f.UpdateToXML("images/"+ fileName,username);
        } else {
            f.UpdateToXML(" ",username);
        }
//        String path = getServlet().getServletContext().getRealPath("/");
//        String tindangFullXML = path + "xml/TindangFull.xml";
//        String loainhaFullXML = path + "xml/TblLoaiNha.xml";
//        String loaigiaytoFullXML = path + "xml/TblLoaiGiayTo.xml";
//        String khuvucFullXML = path + "xml/TblKhuVuc.xml";
//        String loaitinFullXML = path + "xml/TblLoaiTin.xml";
//
//        DatabaseAndXMLFileMappingSyncrolization syn = new DatabaseAndXMLFileMappingSyncrolization();
//        syn.InsertUpdateDelete(tindangFullXML, loainhaFullXML, loaigiaytoFullXML,
//                khuvucFullXML, loaitinFullXML);

        f.recordForReport(username);

        return mapping.findForward(SUCCESS);
    }
}
